package cn.yitun.chat.yitunserverimapicore.business.contact.service.impl;

import cn.yitun.chat.yituncommonmessage.ExceptionEnum;
import cn.yitun.chat.yituncommonmessage.result.ResultMessage;
import cn.yitun.chat.yituncommonutil.YitunMapUtil;
import cn.yitun.chat.yitunservercommonlib.constant.Constants;
import cn.yitun.chat.yitunservercommonlib.crud.yitun.CrudServiceImpl;
import cn.yitun.chat.yitunservercommonlib.dataentity.dto.contact.ContactCategoryDto;
import cn.yitun.chat.yitunservercommonlib.dataentity.po.contact.ContactCategoryInfo;
import cn.yitun.chat.yitunservercommonlib.dataentity.vo.contact.ContactCategoryVo;
import cn.yitun.chat.yitunservercommonlib.enums.groupChat.GroupCategoryDefaultEnums;
import cn.yitun.chat.yitunservercommonlib.util.R;
import cn.yitun.chat.yitunservercommonlib.util.codec.IdGen;
import cn.yitun.chat.yitunserverimapicore.business.contact.service.ContactCategoryService;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactCategoryDao;
import cn.yitun.chat.yitunserverimbase.dao.contact.ContactRelationDao;
import cn.yitun.chat.yitunserverimcore.general.main.biz.contact.manager.ContactCategoryManager;
import cn.yitun.chat.yitunserverimcore.general.main.push.contact.ContactCategoryPush;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 
 * Date 2019-01-20 14:01:41<br>
 * Description
 *
 * @author yitun<br>
 * @version 1.1
 */
@Service
@Transactional
public class ContactCategoryServiceImpl
		extends CrudServiceImpl<ContactCategoryDao, ContactCategoryDto, ContactCategoryInfo, ContactCategoryVo>
		implements ContactCategoryService {

	@Resource
	private ContactCategoryPush contactCategoryPush;
	@Resource
	private ContactRelationDao contactRelationDAO;
	@Resource
	private ContactCategoryManager contactCategoryManager;

	/**
	 * 
	 * Date 2019-01-20 14:09:57<br>
	 * Description 获取所有分组
	 * 
	 * @param userId
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public List<ContactCategoryVo> getListByUserId(Long userId) {
		List<ContactCategoryVo> list = dao.listVO(R.param("userId", userId));
		return list;
	}

	/**
	 * 获取详情
	 * @param userId
	 * @param id
	 * @return
	 */
	public ContactCategoryVo getById(Long userId, Long id) {
		return dao.getVO(id);
	}

	/**
	 * Date 2019-01-20 14:04:32<br>
	 * Description 新增好友分组
	 * @param contactCategory
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage add(ContactCategoryDto contactCategory) {
		ResultMessage message = new ResultMessage();
		if (dao.count(YitunMapUtil.java2Map(contactCategory, false)) > 0) {
			message.addError(ExceptionEnum.NAME_IS_REPETITION);
			return message;
		}

		if (null != contactCategory) {
			try {

				int maxCount = Constants.CONTACT_CATEGORY_MAX_COUNT;
				Long userId = contactCategory.getUserId();
				int count = dao.count(R.param("userId", userId));
				if (count >= maxCount) {
					message.addWarning(ExceptionEnum.SOCK_CONTACT_CATEGORY_OVER_LIMIT);
				} else {
					int rank = count + 1;
					contactCategory.setRank(rank);
					contactCategory.setSort(GroupCategoryDefaultEnums.SORT_CUSTOM.getType());
					contactCategory.setCreateBy(userId);
					contactCategory.setUpdateBy(userId);
					dao.save(contactCategory);
					message.bodyPut("category", contactCategory);

					contactCategoryPush.pushAdd(IdGen.getNumber(), contactCategory.getId(), userId);
				}
			} catch (Exception e) {
				e.printStackTrace();
				message.addWarning(ExceptionEnum.BUSY_SERVER);
			}
		} else {
			message.addWarning(ExceptionEnum.BUSY_SERVER);
		}
		return message;
	}

	/**
	 * Date 2019-01-20 14:06:12<br>
	 * Description 修改分组名称
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage updateName(ContactCategoryDto query) {
		dao.update(query);
		ContactCategoryVo vo = getVO(query.getId());
		contactCategoryPush.pushUpdateName(IdGen.getNumber(), vo.getId(), vo.getName(), vo.getUserId());
		return new ResultMessage(R.param("category", vo));
	}

	/**
	 * Date 2019-01-20 17:26:21<br>
	 * Description 修改排序
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage updateRank(ContactCategoryDto query) {
		ResultMessage message = new ResultMessage();
		List<ContactCategoryVo> list = dao.listVO(R.param("userId", query.getUserId()));
		int size = list.size();

		if (query.getRank() < size) {

			int temp = 0;

			for (int i = 0; i < size; i++) {
				ContactCategoryVo m = list.get(i);
				if (!m.getId().equals(query.getId())) {
					if (temp == query.getRank()) {
						temp++ ;
					}
					dao.updateRank(m.getId(), temp);
					temp++ ;
				}
			}
			dao.updateRank(query.getId(), query.getRank());
		}

		contactCategoryPush.pushUpdateRank(IdGen.getNumber(), query.getUserId());
		return message;
	}

	/**
	 * Date 2019-01-20 16:56:33<br>
	 * Description
	 * @param query
	 * @return
	 * @author yitun<br>
	 * @version 1.1
	 */
	public ResultMessage delete(ContactCategoryDto query) {
		ResultMessage message = new ResultMessage();
		ContactCategoryVo category = dao.getVO(query.getId());
		if (null != category) {
			if (GroupCategoryDefaultEnums.SORT_DEFAULT.getType() == category.getSort()) {
				message.addWarning(ExceptionEnum.SOCK_CONTACT_CATEGORY_CANT_REMOVE_DEFAULT);
			} else {
				dao.remove(query.getId());
				Long newCategoryId = contactCategoryManager.getOrCreateDefaultCategoryId(query.getUserId());
				contactRelationDAO.updateChangeCategoryId(query.getUserId(), query.getId(), newCategoryId);
				contactCategoryPush.pushDelete(IdGen.getNumber(), query.getId(), query.getUserId());
			}
		}
		return message;
	}
}
